/*
 *  main.cpp
 *
 *  Created on: 12.04.2014
 *      Author: Ralph Schurade
 */

#include "game/game.h"
#include "game/config.h"

#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QDateTime>

QTextStream *out = 0;
bool logToFile = false;
bool verbose = false;

void logOutput( QtMsgType type, const QMessageLogContext& context, const QString &message )
{
    QString filedate = QDateTime::currentDateTime().toString( "yyyy.MM.dd hh:mm:ss:zzz" );
    QString debugdate = QDateTime::currentDateTime().toString( "hh:mm:ss:zzz" );

    switch ( type )
    {
    case QtDebugMsg:
        debugdate += " [D]";
        break;
    case QtWarningMsg:
        debugdate += " [W]";
        break;
    case QtCriticalMsg:
        debugdate += " [C]";
        break;
    case QtFatalMsg:
        debugdate += " [F]";
        break;
    }
    if ( verbose )
    {
        (*out) << debugdate << " " << message << endl;
    }
    if ( logToFile )
    {
        QFile outFile("log.txt");
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&outFile);
        ts << filedate << " " << message << endl;
    }

}

void noOutput( QtMsgType type, const QMessageLogContext& context, const QString &message )
{
}

int main( int argc, char *argv[] )
{
    QApplication app( argc, argv );

    QCoreApplication::setOrganizationDomain( "eumel.de" );
	QCoreApplication::setOrganizationName( "Eumel" );
	QCoreApplication::setApplicationName( "gnomes" );
	QCoreApplication::setApplicationVersion( "0.0.1" );

    QStringList args = app.arguments();

    for ( int i = 1; i < args.size(); ++i )
    {
        if ( args.at( i ) == "-h" || args.at( i ) == "?" )
        {
            qDebug() << "Command line options:";
            qDebug() << "-h : displays this message";
            qDebug() << "-v : toggles verbose mode, warning: this will spam your console with messages";
            qDebug() << "-l : logs debug messages to text file";
            qDebug() << "---";
        }
        if ( args.at( i ) == "-v" )
        {
            verbose = true;
        }
        if ( args.at( i ) == "-l" )
        {
            logToFile = true;
        }
    }

    qInstallMessageHandler( noOutput );
    out = new QTextStream( stdout );
    qInstallMessageHandler( logOutput );

    qDebug() << "test";

    if( !Config::getInstance().init() )
    {
        return 0;
    }

    Game* game = new Game();
    if( !game->init() )
    {
        return 0;
    }

    return 0; //app.exec();
}
